﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using pinche_api.Data;
using pinche_api.Models;

namespace pinche_api.Controllers {
    /// <summary>
    ///  用户信息
    /// </summary>
    public class UserController:BaseController {
        private readonly SQLiteContext _context;
        /// <summary>
        /// 用户信息
        /// </summary>
        /// <param name="context"></param>
        public UserController(SQLiteContext context) {
            _context=context;
        }
        /// <summary>
        ///  获取所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<User>>> GetUser() {
            return await _context.User.ToListAsync();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserPaging(int page = 1) {
            var list = await _context.User.OrderBy(p => p.dateline).ToListAsync();
            int pageSize = 20;
            var items = list.Skip((page-1)*pageSize).Take(pageSize).ToList();
            return Content(new {
                status = "1",
                msg = "获取成功",
                list = items
            }.ToJson());
        }
        /// <summary>
        /// 根据openid获取用户信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        [HttpGet("{openid}")]
        public async Task<ActionResult<User>> GetUser(string openid) {
            var user = await _context.User.FirstOrDefaultAsync(e => e.openId==openid);
            if(user==null) {
                return NotFound();
            }
            return user;
        }
        /// <summary>
        ///  修改用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<Result> EditUser([FromForm] User user) {
            Result result = new Result();
            try {
                _context.Entry(user).State=EntityState.Modified;
                _context.SaveChanges();
                result.success=true;
                result.msg="OK";
            } catch(Exception ex) {
                result.success=false;
                result.msg=ex.Message;
            }
            return Ok(result);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<Result> AddUser([FromForm] User user) {
            Result result = new Result();
            try {
                _context.User.Add(user);
                _context.SaveChanges();
                result.success=true;
                result.msg="OK";
            } catch(Exception ex) {
                result.success=false;
                result.msg=ex.Message;
            }
            return Ok(result);
        }

        /// <summary>
        ///  删除用户
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public ActionResult<Result> DeleteUser(string userid) {
            Result result = new Result();
            try {
                var user = _context.User.FirstOrDefault(e => e.id==userid);
                if(user==null) {
                    result.success=false;
                    result.msg="用户不存在";
                    return NotFound(result);
                }
                _context.User.Remove(user);
                _context.SaveChanges();
                result.success=true;
                result.msg="OK";
            } catch(Exception ex) {
                result.success=false;
                result.msg=ex.Message;
            }
            return Ok(result);
        }
    }
}
